package com.huawei.push.util;
 
 import com.huawei.push.util.UnsupportProtocolException;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.security.KeyManagementException;
 import java.security.NoSuchAlgorithmException;
 import java.security.NoSuchProviderException;
 import java.security.Provider;
 import java.security.SecureRandom;
 import java.security.Security;
 import java.util.Locale;
 import java.util.concurrent.TimeUnit;
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.KeyManager;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSocketFactory;
 import javax.net.ssl.TrustManager;
 import javax.net.ssl.X509TrustManager;
 import okhttp3.OkHttpClient;
 import org.apache.http.client.HttpClient;
 import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class SSLCipherSuiteUtil {
   private static final Logger LOGGER = LoggerFactory.getLogger(com.huawei.push.util.SSLCipherSuiteUtil.class);
   
   private static CloseableHttpClient httpClient;
   
   private static OkHttpClient okHttpClient;
   
   public static HttpClient createHttpClient(String protocol) throws Exception {
     SSLContext sslContext = getSslContext(protocol);
     SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new String[] { protocol }, null, (HostnameVerifier)new TrustAllHostnameVerifier(null));
     httpClient = HttpClients.custom().setSSLSocketFactory((LayeredConnectionSocketFactory)sslConnectionSocketFactory).build();
     return (HttpClient)httpClient;
   }
   
   public static OkHttpClient createOkHttpClient(String protocol) throws Exception {
     SSLContext sslContext = getSslContext(protocol);
     SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
     OkHttpClient.Builder builder = (new OkHttpClient.Builder()).sslSocketFactory(sslSocketFactory, (X509TrustManager)new TrustAllManager()).hostnameVerifier((HostnameVerifier)new TrustAllHostnameVerifier(null));
     okHttpClient = builder.connectTimeout(10L, TimeUnit.SECONDS).readTimeout(60L, TimeUnit.SECONDS).build();
     return okHttpClient;
   }
   
   public static HttpURLConnection createHttpsOrHttpURLConnection(URL uUrl, String protocol) throws Exception {
     if (uUrl.getProtocol().toUpperCase(Locale.getDefault()).equals("HTTPS")) {
       SSLContext sslContext = getSslContext(protocol);
       HttpsURLConnection.setDefaultHostnameVerifier((HostnameVerifier)new TrustAllHostnameVerifier(null));
       HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
       return (HttpsURLConnection)uUrl.openConnection();
     } 
     return (HttpURLConnection)uUrl.openConnection();
   }
   
   private static SSLContext getSslContext(String protocol) throws UnsupportProtocolException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
     if (!"TLSv1.2".equals(protocol)) {
       LOGGER.info("Unsupport protocol: {}, Only support TLSv1.2", protocol);
       throw new UnsupportProtocolException("Unsupport protocol, Only support TLSv1.2");
     } 
     TrustAllManager[] trust = { new TrustAllManager() };
     KeyManager[] kms = null;
     SSLContext sslContext = SSLContext.getInstance("TLSv1.2", "SunJSSE");
     SecureRandom secureRandom = new SecureRandom();
     sslContext.init(kms, (TrustManager[])trust, secureRandom);
     sslContext.getServerSessionContext().setSessionCacheSize(8192);
     sslContext.getServerSessionContext().setSessionTimeout(3600);
     return sslContext;
   }
 }